<?php

namespace Wx\Controller;

use Think\Controller;



class MyAddressController extends CommonController {

    // 初始化方法
    public function _initialize(){
        $this->u_id = session('u_id');
        $this->addr = M('address');
    }//end fun _initialize()

    public function test(){
        $this -> display();
    }

//    普通用户-地址管理
    public function index(){
        $address = M('address');
        $list = $address->where("u_id=".session("u_id"))->order('is_default desc')->select();
        foreach($list as $k => $v){
            $list[$k]['a_1_name'] = M('area')->where('id='.$v['a_id_1'])->getField('name');
            $list[$k]['a_2_name'] = M('area')->where('id='.$v['a_id_2'])->getField('name');
            $list[$k]['a_3_name'] = M('area')->where('id='.$v['a_id_3'])->getField('name');
        }
        $this -> assign('list',$list);
        $this -> display('addressManage');
    }// end func index()


    // 添加修改地址信息
    public function addrInfo(){
        $id = intval(I('id'));
        if (IS_POST) {
            $data = I('post.');
            $a_id=explode('-',$data['a_id']);//dump($a_id);die;
            $data['a_id_1']=(int)$a_id[0];
            $data['a_id_2']=(int)$a_id[1];
            $data['a_id_3']=(int)$a_id[2];
            if ($data['id']!='') {//编辑
                $res = $this->edit($data);
            } else {//添加
                $res = $this->add($data);
            }// end if
            if ($res) {
                $this->success('保存成功', U('Wx/MyAddress/index'));
            } else {
                $this->error('保存失败');
            }// end if
        }else{
            if ($id) {
                $data['addrInfo'] = $this->addr->where(array('id'=>$id,'u_id'=>$this->u_id))->find();
                $data['addrInfo']['a_1_name'] = M('area')->where('id='.$data['addrInfo']['a_id_1'])->getField('name');
                $data['addrInfo']['a_2_name'] = M('area')->where('id='.$data['addrInfo']['a_id_2'])->getField('name');
                $data['addrInfo']['a_3_name'] = M('area')->where('id='.$data['addrInfo']['a_id_3'])->getField('name');
                $data['title'] = '编辑地址';
            } else {
                $data['addrInfo'] = 1;
                $data['title'] = '新增地址';
            }
            // 查询一级-省的地址信息
            //地址js生成
            $ali=set_area_js();
            $this->assign('ali',$ali);

            $data['province'] = M('area')->where('lev = 1')->select();
            $this->assign($data);
            $this->display('address');

        }// end if

    }//end fun addrInfo()


    // 添加修改地址信息
    public function add($data){
        $count = $this->addr->where('u_id = '. $this->u_id)->count();
        if($count==5){
            $this->error('您的收货地址已经有五个了');
        }
        $data['u_id'] = $this->u_id;
        $data['add_time'] = time();
        $res = $this->addr->add($data);
        return $res;

    }//end fun addAddr()


    // 添加修改地址信息
    public function edit($data){
        $id = intval($data['id']);
        $map['id'] = $id;
        $map['u_id'] = $this->u_id;
        $res = $this->addr->where($map)->save($data);
        return $res;
    }//end fun editAddr()


    //获取下一级所有地址
    public function getnext(){
        if(IS_AJAX){
            $area = D('area');
            $getall = $area->where("`re_id` =".$_GET['rid'])->select();
            $this->ajaxReturn($getall);
        }else{
            $this->error('非法进入');
        }// end if

    }//end fun getnext()


    // 删除地址
    public function delete(){
        if(IS_POST){
            $addr_id = intval(I('addr_id'));
            $res = $this->addr->where(array('id'=>$addr_id,'u_id'=>$this->u_id))->delete();
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('删除成功，请检查');
            }
        }else{
            $this->error('非法进入');
        }// end if

    }// end fun delete()


    // 修改默认地址
    public function changeDefault(){
        if(IS_POST){
            // 获取设置为默认地址的id
            $addr_id = intval(I('addr_id'));
            // 查询原默认地址信息
            $default = getInfo('address',array('is_default'=>1,'u_id'=>$this->u_id),'u_id,is_default,id');
            // 修改默认 和 原默认
            $res = $this->addr->where('id = '.$addr_id)->setField('is_default',1);
            if($default){
                $res1 = $this->addr->where('id = '.$default['id'])->setField('is_default',0);
            }
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败，请检查');
            }
        }else{
            $this->error('非法进入');
        }// end if

    }



}